package com.example.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.train.business.domain.Daily_TrainStation;
import com.example.train.business.domain.Daily_TrainStationExample;
import com.example.train.business.domain.Train_Station;
import com.example.train.business.mapper.Daily_TrainStationMapper;
import com.example.train.business.req.Daily_TrainStationQueryReq;
import com.example.train.business.req.Daily_TrainStationSaveReq;
import com.example.train.business.resp.Daily_TrainStationQueryResp;
import com.example.train.common.resp.PageResp;
import com.example.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Daily_TrainStationService {
    private static final Logger LOG = LoggerFactory.getLogger(Daily_TrainStationService.class);

    @Resource
    private Daily_TrainStationMapper daily_TrainStationMapper;

    @Resource
    private Train_StationService train_StationService;


    /**
     * 乘客注册请求的服务类
     * @param req
     */
    public void save(Daily_TrainStationSaveReq req) {
        DateTime now = DateTime.now();
        Daily_TrainStation daily_TrainStation = BeanUtil.copyProperties(req, Daily_TrainStation.class);
        if(ObjectUtil.isNull(daily_TrainStation.getId())){
            //在service中对以下三种数据赋值，因为请求参数不带这三个值，需要服务端自行设置
            daily_TrainStation.setId(SnowUtil.getSnowflakeNextId());
            daily_TrainStation.setCreateTime(now);
            daily_TrainStation.setUpdateTime(now);
            daily_TrainStationMapper.insert(daily_TrainStation);
        }else{
            daily_TrainStation.setUpdateTime(now);
            daily_TrainStationMapper.updateByPrimaryKey(daily_TrainStation);
        }
    }
    public PageResp<Daily_TrainStationQueryResp> queryList(Daily_TrainStationQueryReq req) {
        Daily_TrainStationExample daily_TrainStationExample = new Daily_TrainStationExample();
        daily_TrainStationExample.setOrderByClause("date asc,id asc");
        Daily_TrainStationExample.Criteria criteria = daily_TrainStationExample.createCriteria();
        if(ObjectUtil.isNotNull(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        LOG.info("查询页码是 {}", req.getPage());
        LOG.info("每页条数是 {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Daily_TrainStation> daily_TrainStationList = daily_TrainStationMapper.selectByExample(daily_TrainStationExample);

        PageInfo<Daily_TrainStation> pageInfo = new PageInfo<>(daily_TrainStationList);
        LOG.info("总行数是 {}", pageInfo.getTotal());
        LOG.info("总页数是 {}", pageInfo.getPages());

        List<Daily_TrainStationQueryResp> list = BeanUtil.copyToList(daily_TrainStationList, Daily_TrainStationQueryResp.class);

        PageResp<Daily_TrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 删除乘客信息的服务实现
     * @param id
     */
    public void delete(Long id) {
        daily_TrainStationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据车站编号和日期选择自动生成每天的车次信息
     * @param date
     * @param trainCode
     */
    public void genDaily(Date date, String trainCode) {
        LOG.info("生成日期【{}】车次【{}】的车站信息开始", DateUtil.formatDate(date), trainCode);

        // 删除某日某车次的车站信息
        Daily_TrainStationExample dailyTrainStationExample = new Daily_TrainStationExample();
        dailyTrainStationExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);
        daily_TrainStationMapper.deleteByExample(dailyTrainStationExample);

        // 查出某车次的所有的车站信息
        List<Train_Station> stationList = train_StationService.selectByTrainCode(trainCode);
        if (CollUtil.isEmpty(stationList)) {
            LOG.info("该车次没有车站基础数据，生成该车次的车站信息结束");
            return;
        }

        for (Train_Station trainStation : stationList) {
            DateTime now = DateTime.now();
            Daily_TrainStation dailyTrainStation = BeanUtil.copyProperties(trainStation, Daily_TrainStation.class);
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainStation.setCreateTime(now);
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStation.setDate(date);
            daily_TrainStationMapper.insert(dailyTrainStation);
        }
        LOG.info("生成日期【{}】车次【{}】的车站信息结束", DateUtil.formatDate(date), trainCode);
    }

}
